﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Backend.JYD.Config.Cache;
using Backend.JYD.Config;

namespace Backend.JYD.Cache
{
    public sealed class CacheMgr
    {
        private ICache m_Cache = null;
        private static object _lockPad = new object();
        

        private CacheMgr()
        {
            this.m_Cache = this.CreateCache();
          
        }
        public CacheConfig Config
        {
            get
            {
                if (AppConfigs<CacheConfig>.Instance.Configs != null && AppConfigs<CacheConfig>.Instance.Configs.Count > 0)
                    return AppConfigs<CacheConfig>.Instance.Configs.Values.First();
                return new CacheConfig();
            }
        }

        #region Nested Instance Provider

        private class NestedInstanceProvider
        {
            internal static readonly CacheMgr Instance = new CacheMgr();
            // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
            static NestedInstanceProvider() { }
        }

        #endregion

        #region Properties

        public static CacheMgr Instance
        {
            get
            {
                return NestedInstanceProvider.Instance;
            }
        }
        #endregion

        #region Methods

        private ICache CreateCache()
        {
            if (this.Config.CacheType ==-1) return null;
            if (m_Cache == null)
            {
                lock (_lockPad)
                {
                    if (m_Cache == null)
                    {
                        if (this.Config.CacheType == 1)
                            m_Cache = new MemcachedCache();
                        else if (this.Config.CacheType != -1)
                            m_Cache = new HttpCache();
                        else m_Cache = null;
                    }
                }
            }
            return m_Cache;
        }

        public void DeleteCacheItem(string cacheKey)
        {
            if (this.Config.CacheType == -1) return;
            m_Cache.DeleteCacheItem(cacheKey);
        }

        public T GetCachedItem<T>(string cacheKey)
        {
            if (this.Config.CacheType == -1) return default(T);
            try
            {
                return m_Cache.GetCachedItem<T>(cacheKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetCachedItem<T>(string cacheKey, T cacheObject)
        {
            if (this.Config.CacheType == -1) return;
            m_Cache.SetCachedItem(cacheKey, cacheObject);
        }

        public void ClearCache()
        {
            if (this.Config.CacheType == -1) return;
            m_Cache.ClearCache();
        }

        #endregion
    }

   
}
